package me.helllp.dake.core.utils;

import com.alibaba.fastjson2.JSONObject;
import me.helllp.dake.core.DakeCommon;
import me.helllp.dake.core.bean.DakeAuthAccount;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class AccountUtil {
    /**
     * 获取当前登录的用户
     *
     * @return 当前登录的用户信息
     */
    public static DakeAuthAccount toAuthAccount() {
        HttpServletRequest servletRequest = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
        // JSON格式的用户信息
        String userInfo = servletRequest.getHeader(DakeCommon.HEADER_USER_KEY);
        return StringUtils.isEmpty(userInfo) ? null : JSONObject.parseObject(userInfo, DakeAuthAccount.class);
    }
}
